#include "air780e.h"
#include "air780e_common.h"

#include "common_api.h"
#include "luat_rtos.h"
#include "luat_debug.h"

#include "luat_gpio.h"
#include "luat_spi.h"
#include "sfud.h"
#include "luat_fs.h"
#include "common_api.h"
#include "luat_mem.h"
#include "lfs.h"



char* air780e_ftoa(PikaObj* self, pika_float num, int precision) {
    char* result;
    char* str1 = float_to_string(num, precision);
    result = obj_cacheStr(self, str1);
    pikaFree(str1, FTOA_MAX_LENGTH);
    return result;
}



extern luat_spi_t sfud_spi_flash;
luat_spi_t sfud_spi_flash = {
    .id = 0,
    .CPHA = 0,
    .CPOL = 0,
    .dataw = 8,
    .bit_dict = 0,
    .master = 1,
    .mode = 0,
    .bandrate=25600000,
    .cs = 8
};


int air780e_sfud_init2(PikaObj *self){
    int res = -1;
    res = luat_spi_setup(&sfud_spi_flash);
    // __platform_printf("luat_spi_setup res: %d\n", res);
    res = sfud_init();
    __platform_printf("sfud_init res: %d\n", res);
    if (res != 0){
        obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
        obj_setSysOut(self, "[ERROR] sfud_init error is %d.", res);
        return -1;
    }
    return 0;
}



int air780e_sfud_init(PikaObj *self, PikaObj* spi_obj){
    pika_dev* dev = obj_getPtr(spi_obj, "pika_dev");
    luat_spi_t* __spi_cfg = dev->platform_data;
    sfud_spi_flash = *__spi_cfg;
    __platform_printf("### id:%d\n", __spi_cfg->id);
    __platform_printf("### CPHA:%d\n", __spi_cfg->CPHA);
    __platform_printf("### CPOL:%d\n", __spi_cfg->CPOL);
    __platform_printf("### dataw:%d\n", __spi_cfg->dataw);
    __platform_printf("### bit_dict:%d\n", __spi_cfg->bit_dict);
    __platform_printf("### master:%d\n", __spi_cfg->master);
    __platform_printf("### mode:%d\n", __spi_cfg->mode);
    __platform_printf("### bandrate:%d\n", __spi_cfg->bandrate);
    __platform_printf("### cs:%d\n", __spi_cfg->cs);
    // luat_spi_setup(&sfud_spi_flash);
    int res = sfud_init();
    if (res != 0){
        obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
        obj_setSysOut(self, "[ERROR] sfud_init error is %d.", res);
        return -1;
    }
    return 0;
}


int air780e_fs_mount(PikaObj *self, char* type, char* point){
    __platform_printf("%s %s\n", type, point);

    const sfud_flash *flash = sfud_get_device_table();
    luat_fs_init();
    lfs_t* lfs = flash_lfs_sfud(flash, 0, 0);
    if (lfs) {
	    luat_fs_conf_t conf = {
		    .busname = (char*)lfs,
		    .type = type, // "lfs2"
		    .filesystem = type, // "lfs2"
		    .mount_point = point,
	    };
	    int ret = luat_fs_mount(&conf);
        __platform_printf("vfs mount %s ret %d", point, ret);
        if (ret < 0){
            goto __error;
        }
    }
    else {
        __platform_printf("flash_lfs_sfud error");
        goto __error;
    }

    return;

    __error:
    obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
    obj_setSysOut(self, "[ERROR] sfud_init error, check spi flash configure.");

}

void air780e_print(PikaObj *self, char* msg){
    luat_debug_print("<%s> [%d]: %s", __FUNCTION__,__LINE__, msg);
}

